﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.Configuration;

namespace Akronus.TaskScheduler
{
    /// <summary>
    /// Implementation of custom configuration handler for tasks.
    /// </summary>
    public class TaskConfigurationSectionHandler : IConfigurationSectionHandler
    {
        /// <summary>
        /// Creates the specified parent.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="context">The context.</param>
        /// <param name="node">The node.</param>
        /// <returns>The created section handler object.</returns>
        public virtual object Create(object parent, object context, XmlNode node)
        {
            string connection = node.Attributes.GetAttributeValue("taskStateConnectionStringName");
            bool allowRunningTaskWithSameType = bool.Parse(node.Attributes.GetAttributeValue("allowRunningTaskWithSameType"));
            return new TaskConfiguration()
            {
                TaskStateConnectionStringName = connection,
                AllowRunningTaskWithSameType = allowRunningTaskWithSameType,
                Tasks = node.SelectNodes("./task")
            };
        }
    }
}
